package gov.mmgt.netmanager.ui;

import gov.mmgt.netmanager.common.Constant;
import gov.mmgt.netmanager.common.exception.MyException;
import gov.mmgt.netmanager.operate.PropertiesOperate;
import gov.mmgt.netmanager.operate.TestDatabase;
import gov.mmgt.netmanager.ui.common.MyDialog;
import gov.mmgt.netmanager.ui.util.UIUtil;
import gov.mmgt.netmanager.vo.DataSourceVO;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class NetConnDialog extends Dialog {

	private Logger log = Logger.getLogger(getClass());
	
	protected Object result;
	protected Shell shell;
	private Label closeBtn;
	private Text dataSource;
	private Button saveBtn;
	private Button testConnBtn;
	private Button selectFileBtn;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public NetConnDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		
		//读取数据源路径
		dataSource.setText(Constant.PARAMETER.getDataSource());
		
		// 鼠标拖动
		UIUtil.drag(shell);
		shell.setTabList(new Control[]{selectFileBtn, saveBtn, testConnBtn, closeBtn});
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.NO_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(345, 191);
		InputStream is = this.getClass().getResourceAsStream(
				"images/netConnDialog.png");
		shell.setBackgroundImage(new Image(Display.getDefault(), is));
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		
		// 窗口居中
		UIUtil.moveWinToMiddle(shell);
		
		//===============================关闭按钮===============================//
		closeBtn = new Label(shell, SWT.NONE);
		closeBtn.setToolTipText("关闭");
		closeBtn.setBounds(305, 0, 31, 21);
		is = this.getClass().getResourceAsStream("images/close_off.png");
		closeBtn.setImage(new Image(Display.getDefault(), is));
		
		closeBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_on.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_off.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		closeBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.close();
			}
		});
		//===============================关闭按钮===============================//
		
		Label title = new Label(shell, SWT.NONE);
		title.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		title.setBounds(10, 7, 87, 15);
		title.setText("网络连接设置");
		
		Label dataSourceLable = new Label(shell, SWT.RIGHT);
		dataSourceLable.setBounds(5, 83, 87, 15);
		dataSourceLable.setText("数据库连接地址");
		
		//===============================数据源地址===============================//
		dataSource = new Text(shell, SWT.READ_ONLY);
		dataSource.setBounds(98, 79, 209, 21);
		
		is = this.getClass().getResourceAsStream("images/input_1.png");
		dataSource.setBackgroundImage(new Image(Display.getDefault(), is));
		//===============================数据源地址===============================//
		
		//===============================选择数据库文件按钮===============================//
		selectFileBtn = new Button(shell, SWT.NONE);
		selectFileBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(shell, SWT.NONE);
				dialog.setText("请选择数据库文件...");
				dialog.setFilterNames(new String[] {"数据库文件(*.mdb)" });
				dialog.setFilterExtensions(new String[] { "*.mdb" });
				String path = dialog.open();
						
				if (path != null) {
					File file = new File(path);

					if (!file.exists()) {
						MyDialog.alert(shell,"选择的文件不存在");
						return;
					} else {
						dataSource.setText(path);
					}
				}
			}
		});
				
		selectFileBtn.setBounds(314, 78, 21, 23);
		selectFileBtn.setText("...");
		selectFileBtn.setFocus();
		//===============================选择数据库文件按钮===============================//
		
		//===============================保存按钮===============================//
		saveBtn = new Button(shell, SWT.NONE);
		
		//保存数据源地址
		saveBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(dataSource.getText().isEmpty()) {
					MyDialog.alert(shell, "请先选择数据库文件！");
					return;
				}
				
				DataSourceVO dataSourceVo = new DataSourceVO();
				dataSourceVo.setDataSource(dataSource.getText());
				
				//保存数据源地址到配置文件
				String file = "config.ini";
				
				try {
					Constant.PARAMETER.setDataSource(dataSource.getText());
					PropertiesOperate readProperties = new PropertiesOperate(file);
					readProperties.saveParameter(Constant.PARAMETER);
					MyDialog.alert(shell, "数据源保存成功！");
					shell.close();
				} catch (MyException e1) {
					MyDialog.alert(shell, e1.getMessage());
				}
			}
		});
		
		saveBtn.setBounds(80, 142, 80, 26);
		
		is = this.getClass().getResourceAsStream("images/save_off.png");
		saveBtn.setImage(new Image(Display.getDefault(), is));
		
		saveBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/save_on.png");
				saveBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/save_off.png");
				saveBtn.setImage(new Image(Display.getDefault(), is));
			}
		});
		//===============================保存按钮===============================//
		
		//===============================测试数据源按钮===============================//
		testConnBtn = new Button(shell, SWT.NONE);
		
		//测试数据库连接
		testConnBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(dataSource.getText().isEmpty()) {
					MyDialog.alert(shell, "请先选择数据库文件！");
					return;
				}
				
				log.debug("数据库地址：" + dataSource.getText());
				
				try {
					TestDatabase.testDatabase(dataSource.getText());
					MyDialog.alert(shell, "数据库连接成功！");
				} catch (MyException e1) {
					dataSource.setText("");
					MyDialog.alert(shell, e1.getMessage());
				}
			}
		});
		
		testConnBtn.setBounds(205, 142, 80, 26);
		
		is = this.getClass().getResourceAsStream("images/testConn_off.png");
		testConnBtn.setImage(new Image(Display.getDefault(), is));
		
		testConnBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/testConn_on.png");
				testConnBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/testConn_off.png");
				testConnBtn.setImage(new Image(Display.getDefault(), is));
			}
		});
		//===============================测试数据源按钮===============================//
	}
}
